package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.atguigu.gmall.pms.vo.SpuVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.SpuEntity;
import com.atguigu.gmall.pms.service.SpuService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * spu信息
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2022-09-04 10:20:44
 */
@Api(tags = "spu信息 管理")
@RestController
@RequestMapping("pms/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    @GetMapping("page")
    @ApiOperation("search服务远程查询spu分页数据")
    public ResponseVo<List<SpuEntity>> querySpuListByPage(PageParamVo paramVo){
//        PageResultVo pageResultVo = spuService.queryPage(paramVo);
        IPage<SpuEntity> page = spuService.page(paramVo.getPage());
        return ResponseVo.ok(page.getRecords());
    }

    //http://api.gmall.com/pms/spu/category/225?t=1662451311924&pageNum=1&pageSize=10&key=
    @GetMapping("category/{categoryId}")
    public ResponseVo querySpusByCid(@PathVariable("categoryId")String categoryId,
                                     PageParamVo paramVo){
        PageResultVo pageResultVo = spuService.querySpusByCid(categoryId , paramVo);
        return ResponseVo.ok(pageResultVo);
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> querySpuByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = spuService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<SpuEntity> querySpuById(@PathVariable("id") Long id){
		SpuEntity spu = spuService.getById(id);

        return ResponseVo.ok(spu);
    }

    /**
     * 保存
     */
    //http://api.gmall.com/pms/spu
    @PostMapping
    @ApiOperation("保存")
//    public ResponseVo<Object> save(@RequestBody SpuEntity spu){
    public ResponseVo<Object> save(@RequestBody SpuVo spuVo){
        /*
            保存spu的方法虽然接收了前端提交的
            spu、spuattrvalue、spu图片、sku数据 但是当前接口只保存了spu的数据

         */
		spuService.saveSpuVo(spuVo);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody SpuEntity spu){
		spuService.updateById(spu);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		spuService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
